package com.itheima.thread.utils.cyclibarrier.demo02;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class CyclicBarrierThreadUse implements Runnable{

    private CyclicBarrier cyclicBarrier = new CyclicBarrier(2, this);
    private ConcurrentHashMap<Integer, String> concurrentHashMap = new ConcurrentHashMap<>();
//    public CyclicBarrierThreadUse(ConcurrentHashMap<Integer, String> concurrentHashMap) {
//        this.concurrentHashMap = concurrentHashMap;
//    }

    @Override
    public void run() {
        Enumeration<Integer> keys = concurrentHashMap.keys();
        int sum = 0;
        while (keys.hasMoreElements()){
            Integer integer = keys.nextElement();
            sum += integer;
        }
        System.out.println(sum);
    }

    public void count() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bufferedReader = null;
                try {
                    bufferedReader = new BufferedReader(new FileReader("itheima-thread-utils\\src\\com\\itheima\\thread\\utils\\cyclibarrier\\demo02\\1.txt"));
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        concurrentHashMap.put(Integer.parseInt(line), "");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bufferedReader != null){
                        try {
                            bufferedReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println(Thread.currentThread().getName() + "----线程读取数据完毕---");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bufferedReader = null;
                try {
                    bufferedReader = new BufferedReader(new FileReader("itheima-thread-utils\\src\\com\\itheima\\thread\\utils\\cyclibarrier\\demo02\\2.txt"));
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        concurrentHashMap.put(Integer.parseInt(line), "");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bufferedReader != null){
                        try {
                            bufferedReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println(Thread.currentThread().getName() + "----线程读取数据完毕---");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
